/**
 * Software License, Version 1.0
 * 
 * Copyright 2003 The Trustees of Indiana University.  All rights reserved.
 * 
 *
 *Redistribution and use in source and binary forms, with or without 
 *modification, are permitted provided that the following conditions are met:
 *
 *1) All redistributions of source code must retain the above copyright notice,
 * the list of authors in the original source code, this list of conditions and
 * the disclaimer listed in this license;
 *2) All redistributions in binary form must reproduce the above copyright 
 * notice, this list of conditions and the disclaimer listed in this license in
 * the documentation and/or other materials provided with the distribution;
 *3) Any documentation included with all redistributions must include the 
 * following acknowledgement:
 *
 *"This product includes software developed by the Community Grids Lab. For 
 * further information contact the Community Grids Lab at 
 * http://communitygrids.iu.edu/."
 *
 * Alternatively, this acknowledgement may appear in the software itself, and 
 * wherever such third-party acknowledgments normally appear.
 * 
 *4) The name Indiana University or Community Grids Lab or NaradaBrokering, 
 * shall not be used to endorse or promote products derived from this software 
 * without prior written permission from Indiana University.  For written 
 * permission, please contact the Advanced Research and Technology Institute 
 * ("ARTI") at 351 West 10th Street, Indianapolis, Indiana 46202.
 *5) Products derived from this software may not be called NaradaBrokering, 
 * nor may Indiana University or Community Grids Lab or NaradaBrokering appear
 * in their name, without prior written permission of ARTI.
 * 
 *
 * Indiana University provides no reassurances that the source code provided 
 * does not infringe the patent or any other intellectual property rights of 
 * any other entity.  Indiana University disclaims any liability to any 
 * recipient for claims brought by any other entity based on infringement of 
 * intellectual property rights or otherwise.  
 *
 *LICENSEE UNDERSTANDS THAT SOFTWARE IS PROVIDED "AS IS" FOR WHICH NO 
 *WARRANTIES AS TO CAPABILITIES OR ACCURACY ARE MADE. INDIANA UNIVERSITY GIVES
 *NO WARRANTIES AND MAKES NO REPRESENTATION THAT SOFTWARE IS FREE OF 
 *INFRINGEMENT OF THIRD PARTY PATENT, COPYRIGHT, OR OTHER PROPRIETARY RIGHTS. 
 *INDIANA UNIVERSITY MAKES NO WARRANTIES THAT SOFTWARE IS FREE FROM "BUGS", 
 *"VIRUSES", "TROJAN HORSES", "TRAP DOORS", "WORMS", OR OTHER HARMFUL CODE.  
 *LICENSEE ASSUMES THE ENTIRE RISK AS TO THE PERFORMANCE OF SOFTWARE AND/OR 
 *ASSOCIATED MATERIALS, AND TO THE PERFORMANCE AND VALIDITY OF INFORMATION 
 *GENERATED USING SOFTWARE.
 */
package cgl.narada.jms;

import javax.jms.MessageFormatException;
/**
   This is the JMS property conversion table, as specified in the JMS 
   specification. Problems in concersion usually result in the throwing of the
   MessageFormatException.
   
   Column's specify the type that can be read as a tragetted type.
   Thus a byte can only be read as a byte/String
   whereas a short can be returned from a byte, short and String.
   @author Shrideep Pallickara
   $Revision$
   $Date$
*/


public class JmsPropertyConversionTable {
 
  private static String exceptionInfo = 
  "Based on the ConversionTable specified by the JMS-Spec =>";
  
  public static boolean getBooleanValue(Object propertyValue) 
    throws MessageFormatException  {
    if (propertyValue instanceof Boolean) {
      return ((Boolean)propertyValue).booleanValue();
    }
    if (propertyValue instanceof String) {
      String propertyString = (String)propertyValue;
      if (propertyString.equals("true") || propertyString.equals("false")) {
	return (Boolean.valueOf(propertyString)).booleanValue();
      }
    }
    throw new MessageFormatException(exceptionInfo + 
				     "Property conversion to boolean failed");
  }
  

  public static byte getByteValue(Object propertyValue)
    throws MessageFormatException {
    if (propertyValue instanceof Byte)
      return ( (Byte)propertyValue).byteValue();
    
    if (propertyValue instanceof String)
      return Byte.valueOf((String)propertyValue).byteValue();
    
    throw new MessageFormatException(exceptionInfo + 
				     "Property conversion to byte failed");
  }
  
  public static short getShortValue(Object propertyValue) 
    throws MessageFormatException {
    if (propertyValue  instanceof Byte)
      return ( (Byte)propertyValue).shortValue();

    if (propertyValue  instanceof Short)
      return ( (Short)propertyValue).shortValue();

    if (propertyValue instanceof String)
      return Short.valueOf((String)propertyValue).shortValue();

    throw new MessageFormatException(exceptionInfo + 
				     "Property conversion to short failed");
  }
  
  public static int getIntValue(Object propertyValue) 
    throws MessageFormatException {
    if (propertyValue  instanceof Byte)
      return ( (Byte)propertyValue).intValue();

    if (propertyValue  instanceof Short)
      return ( (Short)propertyValue).intValue();
    
    if (propertyValue instanceof Integer)
      return ( (Integer)propertyValue).intValue();
    
    if (propertyValue instanceof String)
      return Integer.parseInt((String)propertyValue);
    
    throw new MessageFormatException(exceptionInfo + 
				     "Property conversion to int failed");
  }
  
  public static long getLongValue(Object propertyValue) 
    throws MessageFormatException {
    if (propertyValue  instanceof Byte)
      return ( (Byte)propertyValue).longValue();

    if (propertyValue  instanceof Short)
      return ( (Short)propertyValue).longValue();
    
    if (propertyValue instanceof Integer)
      return ( (Integer)propertyValue).longValue();

    if (propertyValue instanceof Long)
      return ( (Long)propertyValue).longValue();
    
    if (propertyValue instanceof String)
      return Long.valueOf((String)propertyValue).longValue();
    
    throw new MessageFormatException(exceptionInfo + 
				     "Property conversion to long failed");
  }
  
  
  public static float getFloatValue(Object propertyValue) 
    throws MessageFormatException {
    
    if (propertyValue instanceof Float)
      return ( (Float)propertyValue).floatValue();
    
    if (propertyValue instanceof String) 
      return Float.valueOf((String)propertyValue).floatValue();
    
    throw new MessageFormatException(exceptionInfo + 
				     "Property conversion to float failed");
  }

  public static double getDoubleValue(Object propertyValue) 
    throws MessageFormatException {
    
    if (propertyValue instanceof Float)
      return ( (Float)propertyValue).doubleValue();
    
    if (propertyValue instanceof Double)
      return ( (Double)propertyValue).doubleValue();
    
    if (propertyValue instanceof String) 
      return Double.valueOf((String)propertyValue).doubleValue();
    
    throw new MessageFormatException(exceptionInfo + 
				     "Property conversion to double failed");
  }
  
  
  public static String getStringValue(Object propertyValue) 
    throws MessageFormatException {
    return propertyValue.toString();
  }
  
}
